package com.henry.web;

import org.springframework.stereotype.Component;

import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicReference;

/**
 * Created by 峥桂 on 2017/6/6.
 */
@Component
public class CASUtils1 {
    private String curVal ="";
    private AtomicReference atomicReferencesync = null;

    public boolean updateStateLongSnc(int id,int varl) {
            String currKey= id+"#"+varl;
//        System.out.print("===>>"+AtomicReference);
        return getAtomicReference(currKey).compareAndSet(getCurrLong(),currKey);
    }

    public AtomicReference getAtomicReference(String key) {
        if (atomicReferencesync == null) {
            synchronized (AtomicReference.class) {
                System.out.println(atomicReferencesync);
               if(atomicReferencesync==null){
                   atomicReferencesync = new AtomicReference(getCurrLong());
               }
           }
        }else{
            System.out.println(atomicReferencesync.get().toString());
            if(!atomicReferencesync.get().toString().equals(key)){
                atomicReferencesync = null;
                return getAtomicReference(key);
            }
        }
        return atomicReferencesync;
    }

    public void setCurrLong(String currLong_) {
        curVal = currLong_;
    }

    public String getCurrLong() {
        return curVal;
    }

}
